﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类Notice。
    /// </summary>
    public class Notice
    {
        private const string CONNECTSTRING_SEARCH = "Chenxin_House_Search";
        private const string CONNECTSTRING = "Chenxin_House";

        public Notice()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Notice where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.Notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Notice(");
            strSql.Append("RecordId,NoticeType,Title,body,AttachmentName,AttachmentFileName,AttachmentFilePath,AttachmentFileSize,AttachmentFileType,CompanyId,CompanyName,SenderId,DelFlg,DepartMentId,SendDate,UpdateDate,CityId,ReadFlg)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@NoticeType,@Title,@body,@AttachmentName,@AttachmentFileName,@AttachmentFilePath,@AttachmentFileSize,@AttachmentFileType,@CompanyId,@CompanyName,@SenderId,@DelFlg,@DepartMentId,@SendDate,@UpdateDate,@CityId,@ReadFlg)");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "NoticeType", DbType.Int32, model.NoticeType);
            db.AddInParameter(dbCommand, "Title", DbType.AnsiString, model.Title);
            db.AddInParameter(dbCommand, "body", DbType.AnsiString, model.body);
            db.AddInParameter(dbCommand, "AttachmentName", DbType.AnsiString, model.AttachmentName);
            db.AddInParameter(dbCommand, "AttachmentFileName", DbType.AnsiString, model.AttachmentFileName);
            db.AddInParameter(dbCommand, "AttachmentFilePath", DbType.AnsiString, model.AttachmentFilePath);
            db.AddInParameter(dbCommand, "AttachmentFileSize", DbType.Int32, model.AttachmentFileSize);
            db.AddInParameter(dbCommand, "AttachmentFileType", DbType.AnsiString, model.AttachmentFileType);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "SenderId", DbType.Guid, model.SenderId);
            db.AddInParameter(dbCommand, "DelFlg", DbType.Int32, model.DelFlg);
            db.AddInParameter(dbCommand, "DepartMentId", DbType.Guid, model.DepartMentId);
            db.AddInParameter(dbCommand, "SendDate", DbType.DateTime, model.SendDate);
            db.AddInParameter(dbCommand, "UpdateDate", DbType.DateTime, model.UpdateDate);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "ReadFlg", DbType.Int32, model.ReadFlg);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.Notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Notice set ");
            strSql.Append("NoticeType=@NoticeType,");
            strSql.Append("Title=@Title,");
            strSql.Append("body=@body,");
            strSql.Append("AttachmentName=@AttachmentName,");
            strSql.Append("AttachmentFileName=@AttachmentFileName,");
            strSql.Append("AttachmentFilePath=@AttachmentFilePath,");
            strSql.Append("AttachmentFileSize=@AttachmentFileSize,");
            strSql.Append("AttachmentFileType=@AttachmentFileType,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("SenderId=@SenderId,");
            strSql.Append("DelFlg=@DelFlg,");
            strSql.Append("DepartMentId=@DepartMentId,");
            strSql.Append("SendDate=@SendDate,");
            strSql.Append("UpdateDate=@UpdateDate,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("ReadFlg=@ReadFlg");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "NoticeType", DbType.Int32, model.NoticeType);
            db.AddInParameter(dbCommand, "Title", DbType.AnsiString, model.Title);
            db.AddInParameter(dbCommand, "body", DbType.AnsiString, model.body);
            db.AddInParameter(dbCommand, "AttachmentName", DbType.AnsiString, model.AttachmentName);
            db.AddInParameter(dbCommand, "AttachmentFileName", DbType.AnsiString, model.AttachmentFileName);
            db.AddInParameter(dbCommand, "AttachmentFilePath", DbType.AnsiString, model.AttachmentFilePath);
            db.AddInParameter(dbCommand, "AttachmentFileSize", DbType.Int32, model.AttachmentFileSize);
            db.AddInParameter(dbCommand, "AttachmentFileType", DbType.AnsiString, model.AttachmentFileType);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "SenderId", DbType.Guid, model.SenderId);
            db.AddInParameter(dbCommand, "DelFlg", DbType.Int32, model.DelFlg);
            db.AddInParameter(dbCommand, "DepartMentId", DbType.Guid, model.DepartMentId);
            db.AddInParameter(dbCommand, "SendDate", DbType.DateTime, model.SendDate);
            db.AddInParameter(dbCommand, "UpdateDate", DbType.DateTime, model.UpdateDate);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "ReadFlg", DbType.Int32, model.ReadFlg);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Notice ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.Notice GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,NoticeType,Title,body,AttachmentName,AttachmentFileName,AttachmentFilePath,AttachmentFileSize,AttachmentFileType,CompanyId,CompanyName,SenderId,DelFlg,DepartMentId,SendDate,UpdateDate,CityId,ReadFlg from Notice ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.House.Entity.Notice model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,NoticeType,Title,body,AttachmentName,AttachmentFileName,AttachmentFilePath,AttachmentFileSize,AttachmentFileType,CompanyId,CompanyName,SenderId,DelFlg,DepartMentId,SendDate,UpdateDate,CityId,ReadFlg ");
            strSql.Append(" FROM Notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING);
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Notice");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Notice> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,NoticeType,Title,body,AttachmentName,AttachmentFileName,AttachmentFilePath,AttachmentFileSize,AttachmentFileType,CompanyId,CompanyName,SenderId,DelFlg,DepartMentId,SendDate,UpdateDate,CityId,ReadFlg ");
            strSql.Append(" FROM Notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.Notice> list = new List<Chenxin.Lib.House.Entity.Notice>();
            Database db = DatabaseFactory.CreateDatabase(CONNECTSTRING_SEARCH);
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.Notice ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.Notice model = new Chenxin.Lib.House.Entity.Notice();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["NoticeType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NoticeType = (int)ojb;
            }
            model.Title = dataReader["Title"].ToString();
            model.body = dataReader["body"].ToString();
            model.AttachmentName = dataReader["AttachmentName"].ToString();
            model.AttachmentFileName = dataReader["AttachmentFileName"].ToString();
            model.AttachmentFilePath = dataReader["AttachmentFilePath"].ToString();
            ojb = dataReader["AttachmentFileSize"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AttachmentFileSize = (int)ojb;
            }
            model.AttachmentFileType = dataReader["AttachmentFileType"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["SenderId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SenderId = new Guid(ojb.ToString());
            }
            ojb = dataReader["DelFlg"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DelFlg = (int)ojb;
            }
            ojb = dataReader["DepartMentId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartMentId = new Guid(ojb.ToString());
            }
            ojb = dataReader["SendDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SendDate = (DateTime)ojb;
            }
            ojb = dataReader["UpdateDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateDate = (DateTime)ojb;
            }
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ReadFlg"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReadFlg = (int)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

